import random

from django.shortcuts import render

# Create your views here.
from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from django import http

from apps.verifications import serializers
from libs.captcha.captcha import captcha
from libs.yuntongxun.sms import CCP


class ImageCodeView(APIView):
    #生成一个图像验证码并返回
    def get(self,request,image_code_id):
        #生成图像验证码
        text,image_data =captcha.generate_captcha()

        #将图像验证码存储到session中
        get_conn = get_redis_connection("code")
        get_conn.setex("image_code_%s"%image_code_id,300,text)

        #返回响应
        response = http.HttpResponse(image_data,status=status.HTTP_200_OK)
        response['Content-Type'] = "image/jpg"
        return response

    pass


"""
class SmsCodeView(GenericAPIView):
    serializer_class = "ImageCodeSerializers"
    def get(self,request,mobile):

        #获取参数
        query_data = request.query_params

        #序列化器校验数据
        serializer = self.get_serializer(data = query_data)
        serializer.is_valid(raise_exception = True)

        #判断是否频繁发送短信
        get_conn = get_redis_connection("code")
        # if get_conn.get("sms_time_%s"%mobile):
        #     return Response({"errmsg":"频繁发送"},status=status.HTTP_400_BAD_REQUEST)
        send_flag = get_conn.get("send_flag_%s"%mobile)
        if send_flag:
            return Response({"errmsg":"频繁发送"},status=status.HTTP_400_BAD_REQUEST)

        #发送短信
        sms_code = "%06d"%random.randint(0,999999)
        print("短信验证码是:%s"%sms_code)
        # ccp = CCP()
        # result =  ccp.send_template_sms(mobile, ['123456', 5], 1)
        # if result ==-1:
        #     return http.HttpResponseBadRequest("发送失败")

        #将短信验证码保存到redis中
        get_conn.setex("sms_code_%s"%mobile,300,sms_code)
        get_conn.setex("send_flag_%s"%mobile,60,True)

        #返回响应
        return Response(status=status.HTTP_200_OK)



        pass

    pass
"""

class SmsCodeView(GenericAPIView):
    serializer_class = serializers.SmsCodeSerializer#这里导入的短信序列化器出错
    def get(self,request,mobile):
        #1,获取参数
        dict_data = request.query_params

        #2,创建序列化器,校验
        serializer =  self.get_serializer(data=dict_data)
        serializer.is_valid(raise_exception=True)

        #2,1判断是否频繁发送
        redis_conn = get_redis_connection("code")
        send_flag = redis_conn.get("send_flag_%s"%mobile)
        if send_flag:
            return Response({"errmsg":"频繁发送"},status=status.HTTP_400_BAD_REQUEST)

        #3,生成验证码并打印出来
        sms_code = "%06d"%random.randint(0,99999)
        print("sms_code = %s"%sms_code)

        # 使用celery发送短信任务
        # from celery_tasks.sms.tasks import send_sms_code
        # send_sms_code.delay(mobile,sms_code)

        # 发送短信
        # result = CCP().send_template_sms(mobile,[sms_code,5],1)
        #
        # if result == -1:
        #     return http.HttpResponseBadRequest("发送失败")

        #4,短信入库
        redis_conn.setex("sms_code_%s"%mobile,300,sms_code)
        redis_conn.setex("send_flag_%s"%mobile,60,True)

        #5,返回响应
        return Response(status=status.HTTP_200_OK)
